<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>对账</a>
        <a>
            <cite>商户对账</cite>
        </a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">对账文件</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-row">
                        <div class="layui-form" style="float:right; margin-bottom: 10px;">
                            <div class="layui-form-item" style="margin:0;">
                                <div class="layui-input-inline">
                                    <select name="type" id="state" lay-search="">
                                        <option value="-99">状态</option>
                                        <option value="0">未生成</option>
                                        <option value="1">已生成</option>
                                    </select>
                                </div>
                                <button id="search" class="layui-btn" data-type="reload">搜索</button>
                            </div>
                        </div>
                    </div>
                    <table id="Pay_Mgr_PayOrder_dataAll" lay-filter="Pay_Mgr_PayOrder_dataAll"></table>
                </div>

            </div>
        </div>
    </div>
</div>

<script type="text/html" id="payBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a target="_blank" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="download">下载</a>
</script>

<script>
    layui.use(['admin', 'table', 'util','form'], function () {
        var $ = layui.$,
            admin = layui.admin,
            table = layui.table,
            view =layui.view,
            layer =layui.layer
        element = layui.element,
            form = layui.form;

        element.render('breadcrumb', 'breadcrumb');

        //用户列表
        table.render({
            elem: '#Pay_Mgr_PayOrder_dataAll',
                url: layui.setter.baseUrl + '/bill/list' // 用户列表接口
            ,
            where: {
                access_token: layui.data(layui.setter.tableName).access_token
            },
            id: 'tableReload',
            page: true,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'billDate',
                    title: '对账日期',
                    templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd") }}</div>'
                }, {
                    field: 'state',
                    title: '状态',
                    templet: '<div>{{ d.state == 0?"未生成":"已生成" }}</div>'
                },  {
                    field: 'createTime',
                    title: '创建时间',
                    templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'
                }, {
                    field: 'edit',
                    title: '操作',
                    toolbar: '#payBar'
                }]
            ],
            skin: 'line'
        });

        //监听工具条
        table.on('tool(Pay_Mgr_PayOrder_dataAll)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                var id =data.id;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/bill/get',
                    data: {
                        id: id
                    },
                    error: function (err) {
                        layer.alert(JSON.stringify(err.field), {
                            title: '错误提示'
                        })
                    },
                    success: function (res) {
                        if (res.code == 0) {

                            admin.popup({
                                title: '查看详情'
                                , area: ['390px', '580px']
                                , id: 'LAY-popup-content-view'
                                , success: function (layero, index) {
                                    view(this.id).render('reconciliation/down/view').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');

                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        form.val("layuiadmin-app-form-list",{

                                            "id":data.id,
                                            "mchId":data.mchId,
                                            "state":data.state==0?"未生成":"已生成",
                                            "billDate":layui.util.toDateString(res.data.billDate, "yyyy-MM-dd")


                                        })
                                        $('#mchName').val(res.data.mchName);
                                        $('#mchType').val(res.data.mchType == 1?"平台账户":"私有账户");
                                        $('#billPath').val(res.data.billPath);

                                        if (res.data.createTime != "" && res.data.createTime != null) {
                                            $('#createTime').val(layui.util.toDateString(res.data.createTime));
                                        }
                                        if (res.data.updateTime != "" && res.data.updateTime != null) {
                                            $('#updateTime').val(layui.util.toDateString(res.data.updateTime));
                                        }
                                        form.render();

                                    });
                                }
                            });

                        }
                    }
                })
            }else if(obj.event === 'download') {
                // location.href = data.billPath;
                window.open(data.billPath) ;
            }
        });

        // 搜索
        var $ = layui.$,
            active = {
                reload: function () {
                    var state = $("#state").val();
                    //执行重载
                    table.reload('tableReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        },
                        where: {
                            state: state
                        }
                    });
                }
            };
        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        // 渲染表单
        form.render();
    });
</script>
